﻿/*
    Copyright 2013 Bob Moss (bobbyjmoss@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace OpenRAMS.Model
{
    public class RoomRequests : RoomRequest
    {
        //Both are private - use provided functions only.
        private List<RoomRequest> roomRequests;
        private List<String> roomNames;

        //database-related variables
        private SqlConnection dbConnection;
        private SqlDataReader dbReader;
        private SqlCommand dbCommand;
        private String dbInString;

        //Custom getter to retrieve list
        public List<RoomRequest> getRoomRequests()
        {
            return roomRequests;
        }

        public RoomRequests()
        {
            //db-related initialisations
            initHardCodedVals();
            try { dbInString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; }
            catch (Exception e) { throw e; }

            //initialise lists
            roomRequests = new List<RoomRequest>();
            roomNames = new List<String>();
        }

        public int Populate(int bookingID)
        {
            //re-initialise lists
            roomRequests = new List<RoomRequest>();
            roomNames = new List<String>();

            //Build query - only need a list of creators' usernames, so query has been tailored accordingly
            String queryFull = "SELECT " + attribNames[1] + " FROM " + tableName + " WHERE " + attribNames[0] + "=" + bookingID;

            //Verify the ID paramerer value is sensible
            if (bookingID < 0) { return -1; }

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //import list of resource names from Db
            while (dbReader.Read())
            {
                try
                {
                    roomNames.Add(dbReader[attribNames[1]].ToString());
                }
                catch { return -1; }
            }

            //verify more than one result. If not, error.
            if (roomNames.Count() < 1) { return -1; }

            //Use this list to import resource request records
            foreach (String res in roomNames)
            {
                RoomRequest newRequest = new RoomRequest();
                int importNote = newRequest.Load(bookingID, res);
                if (importNote < 0) { return -1; }  //if failed, bail with error code
                else
                {
                    roomRequests.Add(newRequest); //if all went well, add to list
                }
            }

            //verify more than one result. Else, error (important, as must be at least one resource request per booking)
            if (roomRequests.Count() < 1) { return -1; }

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }

            return 0;
        }


    }
}